package firsttest;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {

    @Test
    public void queryById() {
        SqlSession sqlSession = getSqlSession();
        Student student = sqlSession.selectOne("firsttest.mapper.studentMapper.getStudent", 1);
        System.out.println(student.toString());
    }

    @Test
    public void insertStudent() {
        Student student = new Student();
        student.setAge(30);
        student.setHobby("篮球");
        student.setName("姚明");
        SqlSession sqlSession = getSqlSession();
        int iPos = sqlSession.insert("firsttest.mapper.studentMapper.addStudent", student);
        sqlSession.commit();    //插入操作需要手动的提交，注意！当没有手动提交时，MySql 实际上已经接受到请求，主键已经 ++
        System.out.println(iPos + "");
    }

    @Test
    public void updateStudent() {
        Student student = new Student();
        student.setName("梅西");
        student.setHobby("足球");
        student.setAge(30);
        student.setId(6);
        SqlSession sqlSession = getSqlSession();
        int iPos = sqlSession.update("firsttest.mapper.studentMapper.updateStudent", student);
        sqlSession.commit();
        System.out.println(iPos + "");
    }

    @Test
    public void deleteStudent() {
        SqlSession sqlSession = getSqlSession();
        int iPos = sqlSession.delete("firsttest.mapper.studentMapper.deleteStudent", 4);
        sqlSession.commit();
        System.out.println(iPos + "");
    }

    @Test
    public void getStudentByDy() {
        SqlSession sqlSession = getSqlSession();
        Student student = new Student();
        student.setName("姚明");
        student.setAge(29);
        List<Student> list = sqlSession.selectList("firsttest.mapper.studentMapper.dyFindStudent", student);
        for (Student student1 : list) {
            System.out.println(student1.toString());
        }
    }

    @Test
    public void updateByDy() {
        SqlSession sqlSession = getSqlSession();
        Student student1 = new Student();
        student1.setAge(26);
        student1.setName("易建联");
        student1.setId(7);
        int iState = sqlSession.update("firsttest.mapper.studentMapper.dyUpdateStudent", student1);
        System.out.println(iState + "");
        sqlSession.commit();
    }

    @Test
    public void deleteByDyArr() {
        SqlSession sqlSession = getSqlSession();
        Integer[] arrId = new Integer[] {6, 7};
        int iState = sqlSession.delete("firsttest.mapper.studentMapper.dyDeleteStudentByArr", arrId);
        System.out.println(iState + "");
        sqlSession.commit();
    }

    @Test
    public void deleteByDyList() {
        SqlSession sqlSession = getSqlSession();
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        int iState = sqlSession.delete("firsttest.mapper.studentMapper.dyDeleteStudentByList", list);
        System.out.println(iState + "");
        sqlSession.commit();
    }

    @Test
    public void deleteByDyMap() {
        SqlSession sqlSession = getSqlSession();
        Map<String, Object> map = new HashMap<>();
        map.put("ids", new Integer[]{1, 2, 3});
        map.put("age", 30);
        int iState = sqlSession.delete("firsttest.mapper.studentMapper.dyDeleteStudentByMap", map);
        System.out.println(iState + "");
        sqlSession.commit();
    }

    /**获取 SqlSession 的标准流程
     * 1.指定配置文件的对于项目 src 的相对路径
     * 2.通过 Resources.getResourceAsStream 的对文件进行读取，并以流的方式返回
     * 3.通过 SqlSessionFactoryBuilder 对流的读取构造 SqlSessionFactory
     * 4.通过 SqlSessionFactory　开启一个 SqlSession
     *
     * @return
     */
    public static SqlSession getSqlSession() {
        SqlSession sqlSession = null;
        try {
            String resource = "firsttest/MyBatisCfg.xml";
            InputStream in = Resources.getResourceAsStream(resource);
            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in);
            sqlSession = sessionFactory.openSession();
        } catch (Exception e) {
            sqlSession = null;
        }
        return sqlSession;
    }
}
